
package com.benimetum.managers;

import com.benimetum.database.Sparql;
import com.benimetum.exceptions.DatabaseConnectException;
import com.benimetum.exceptions.DatabaseWriteOrDeleteException;
import com.benimetum.items.*;
import com.benimetum.mail.MailSender;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import java.util.*;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class AnnouncementManager
{
    private final static String RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    private final static String ETU="http://www.benimetum.com/benimetum.owl#";
    /**
     * duyuru oluşturur
     * @param text
     * @param students
     * @param teacherId
     * @return 
     */
    public static boolean createAnnouncement(String text, Person[] students, String teacherId){
	  String[] emails=new String[students.length];
	  try {
		Random random=new Random();
		String id="an"+random.nextInt(10000000);
		Sparql.insertProperty(ETU+id, RDF+"type", ETU+"Announcement");
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"creatorOf", ETU+id);
		Sparql.insertLiteral(ETU+id, ETU+"text", text);
		Sparql.insertLiteral(ETU+id, ETU+"id", id);
		for (int i = 0; i < students.length; i++) {
		    Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+students[i].getId());
		    Sparql.insertProperty(ETU+students[i].getId(), ETU+"hasActivity", ETU+id);
		    emails[i]=students[i].getEmail();
		}
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  String teacherName=TeacherManager.getTeacherById(teacherId).getFirstName();
	  MailSender send=new MailSender(emails, "BenimEtum", "["+teacherName+"] duyuru eklendi",teacherName+" adlı kullanıcı \n"+text+"\n duyurusunu ekledi.");
	  Thread thread=new Thread(send);
	  thread.start();
	  return true;
    }
    
    /**
     * ders duyurusu oluşturur
     * @param text
     * @param students
     * @param teacherId
     * @param courseId
     * @return 
     */
    public static boolean createCourseAnnouncement(String text, Student[] students, String teacherId, String courseId){
	  String[] emails=new String[students.length];
	  try {
		Random random=new Random();
		String id="an"+random.nextInt(10000000);
		Sparql.insertProperty(ETU+id, RDF+"type", ETU+"Announcement");
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+teacherId);
		Sparql.insertProperty(ETU+id, ETU+"ownedBy", ETU+courseId);
		Sparql.insertProperty(ETU+courseId, ETU+"hasAnnouncement", ETU+id);
		Sparql.insertProperty(ETU+teacherId, ETU+"creatorOf", ETU+id);
		Sparql.insertLiteral(ETU+id, ETU+"text", text);
		Sparql.insertLiteral(ETU+id, ETU+"id", id);
		for (int i = 0; i < students.length; i++) {
		    Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+students[i].getId());
		    Sparql.insertProperty(ETU+students[i].getId(), ETU+"hasActivity", ETU+id);
		    emails[i]=students[i].getEmail();
		}
		Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"hasActivity", ETU+id);
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  String courseName=CourseManager.getCourse(courseId).getNAME();
	  MailSender send=new MailSender(emails, "BenimEtum", "["+courseName+"] Dersine etkinlik eklendi", text);
	  Thread thread=new Thread(send);
	  thread.start();
	  return true;
    }
    
    /**
     * grup duyurusu oluşturur
     * @param text
     * @param students
     * @param teacherId
     * @param communityId
     * @return 
     */
    public static boolean createCommunityAnnouncement(String text, Person[] members, String adminID, String communityId){
	  String[] emails=new String[members.length];
	  try {
		Random random=new Random();
		String id="an"+random.nextInt(10000000);
		Sparql.insertProperty(ETU+id, RDF+"type", ETU+"Announcement");
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+adminID);
		Sparql.insertProperty(ETU+id, ETU+"ownedBy", ETU+communityId);
		Sparql.insertProperty(ETU+communityId, ETU+"hasAnnouncement", ETU+id);
		Sparql.insertProperty(ETU+adminID, ETU+"creatorOf", ETU+id);
		Sparql.insertLiteral(ETU+id, ETU+"text", text);
		Sparql.insertLiteral(ETU+id, ETU+"id", id);
		for (int i = 0; i < members.length; i++) {
		    Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+members[i].getId());
		    Sparql.insertProperty(ETU+members[i].getId(), ETU+"hasActivity", ETU+id);	
		    emails[i]=members[i].getEmail();		    
		}
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  MailSender send=new MailSender(emails, "BenimEtum", "["+communityId+"] topluluğuna duyuru Eklendi", text);
	  Thread thread=new Thread(send);
	  thread.start();
	  return true;
    }
    
    /**
     * etkinlik oluşturur
     * @param text
     * @param date
     * @param students
     * @param teacherId
     * @return 
     */
    public static boolean createEvent(String text, Date date, Person[] students, String teacherId){
	  String[] emails=new String[students.length];
	  try {
		Random random=new Random();
		String id="ev"+random.nextInt(10000000);
		Sparql.insertProperty(ETU+id, RDF+"type", ETU+"Event");
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"creatorOf", ETU+id);
		Sparql.insertLiteral(ETU+id, ETU+"text", text);
		Sparql.insertLiteral(ETU+id, ETU+"id", id);
		Sparql.insertLiteral(ETU+id, ETU+"date", ""+date.getTime());
		for (int i = 0; i < students.length; i++) {
		    Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+students[i].getId());
		    Sparql.insertProperty(ETU+students[i].getId(), ETU+"hasActivity", ETU+id);
		    emails[i]=students[i].getEmail();
		}
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  String teacherName=TeacherManager.getTeacherById(teacherId).getFirstName();
	  MailSender send=new MailSender(emails, "BenimEtum", "["+teacherName+"] etkinlik ekledi.", teacherName+" adlı kullanıcı \n"+text+"\n duyurusunu ekledi.");
	  Thread thread=new Thread(send);
	  thread.start();
	  return true;
    }
    
    /**
     * ders etkinliği oluşturur
     * @param text
     * @param date
     * @param students
     * @param teacherId
     * @param courseId
     * @return 
     */
    public static boolean createCourseEvent(String text, Date date, Student[] students, String teacherId, String courseId){
	  String[] emails=new String[students.length];
	  try {
		Random random=new Random();
		String id="ev"+random.nextInt(10000000);
		Sparql.insertProperty(ETU+id, RDF+"type", ETU+"Event");
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+teacherId);
		Sparql.insertProperty(ETU+id, ETU+"ownedBy", ETU+courseId);
		Sparql.insertProperty(ETU+courseId, ETU+"hasAnnouncement", ETU+id);
		Sparql.insertProperty(ETU+teacherId, ETU+"creatorOf", ETU+id);
		Sparql.insertLiteral(ETU+id, ETU+"text", text);
		Sparql.insertLiteral(ETU+id, ETU+"date", ""+date.getTime());
		Sparql.insertLiteral(ETU+id, ETU+"id", id);
		for (int i = 0; i < students.length; i++) {
		    Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+students[i].getId());
		    Sparql.insertProperty(ETU+students[i].getId(), ETU+"hasActivity", ETU+id);
		    emails[i]=students[i].getEmail();
		}
		Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"hasActivity", ETU+id);
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }	  
	  String courseName=CourseManager.getCourse(courseId).getNAME();
	  MailSender send=new MailSender(emails, "BenimEtum", "["+courseName+"] Dersine duyuru Eklendi", text);
	  Thread thread=new Thread(send);
	  thread.start();
	  return true;
    }
    
    /**
     * topluluk etkinliği oluşturur
     * @param text
     * @param date
     * @param students
     * @param teacherId
     * @param communityId
     * @return 
     */
    public static boolean createCommunityEvent(String text, Date date, Person[] members, String adminID, String communityId){
	  String[] emails=new String[members.length];
	  try {
		Random random=new Random();
		String id="ev"+random.nextInt(10000000);
		Sparql.insertProperty(ETU+id, RDF+"type", ETU+"Event");
		Sparql.insertProperty(ETU+id, ETU+"addebBy", ETU+adminID);
		Sparql.insertProperty(ETU+id, ETU+"ownedBy", ETU+communityId);
		Sparql.insertProperty(ETU+communityId, ETU+"hasAnnouncement", ETU+id);
		Sparql.insertProperty(ETU+adminID, ETU+"creatorOf", ETU+id);
		Sparql.insertLiteral(ETU+id, ETU+"text", text);
		Sparql.insertLiteral(ETU+id, ETU+"date", ""+date.getTime());
		Sparql.insertLiteral(ETU+id, ETU+"id", id);
		for (int i = 0; i < members.length; i++) {
		    Sparql.insertProperty(ETU+id, ETU+"sendTo", ETU+members[i].getId());
		    Sparql.insertProperty(ETU+members[i].getId(), ETU+"hasActivity", ETU+id);	
		    emails[i]=members[i].getEmail();		    
		}
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  MailSender send=new MailSender(emails, "BenimEtum", "["+communityId+"] topluluğuna etkinlik Eklendi", text);
	  Thread thread=new Thread(send);
	  thread.start();
	  return true;
    }
    
    /**
     * duyuru siler
     * @param id
     * @return 
     */
    public static boolean deleteAnnouncement(String id){
	  Student[] students=getAnnouncementStudents(id);
	  String teacherId=getAnnouncementTeacher(id).getId();
	  String[] emails=new String[students.length];
	  String courseId=getAnnouncementOwnedBy(id);
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"text");
		Sparql.removeLiteral(ETU+id, ETU+"id");
		for (int i = 0; i < students.length; i++) {
		    Sparql.removeResource(ETU+id, ETU+"sendTo", ETU+students[i].getId());
		    Sparql.removeResource(ETU+students[i].getId(), ETU+"hasActivity", ETU+id);
		    emails[i]=students[i].getEmail();
		}
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"creatorOf", ETU+id);
		if(courseId!=null){
		    Sparql.removeResource(ETU+id, ETU+"ownedBy", ETU+courseId);
		    Sparql.removeResource(ETU+courseId, ETU+"hasAnnouncement", ETU+id);
		}
		Sparql.removeResource(ETU+id, RDF+"type", ETU+"Announcement");
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * etkinlik siler
     * @param id
     * @return 
     */
    public static boolean deleteEvent(String id){
	  Student[] students=getEventsStudents(id);
	  String teacherId=getEventsTeacher(id).getId();
	  String[] emails=new String[students.length];
	  String courseId=getEventOwnedBy(id);
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"text");
		Sparql.removeLiteral(ETU+id, ETU+"id");
		for (int i = 0; i < students.length; i++) {
		    Sparql.removeResource(ETU+id, ETU+"sendTo", ETU+students[i].getId());
		    Sparql.removeResource(ETU+students[i].getId(), ETU+"hasActivity", ETU+id);
		    emails[i]=students[i].getEmail();
		}
		Sparql.insertProperty(ETU+id, ETU+"addedby", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"creatorOf", ETU+id);
		if(courseId!=null){
		    Sparql.removeResource(ETU+id, ETU+"ownedBy", ETU+courseId);
		    Sparql.removeResource(ETU+courseId, ETU+"hasAnnouncement", ETU+id);
		}
		Sparql.removeResource(ETU+id, RDF+"type", ETU+"Event");
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * duyurunun içindeki texti değiştirir.
     * @param id
     * @param newText
     * @return 
     */
    public static boolean updateAnnouncement(String id, String newText){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"text");
		Sparql.insertLiteral(ETU+id, ETU+"text",newText);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * etkinliğin yazısını değiştirir
     * @param id
     * @param newText
     * @return 
     */
    public static boolean updateEvent(String id, String newText){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"text");
		Sparql.insertLiteral(ETU+id, ETU+"text",newText);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static boolean updateEvent(String id, String newText, Date date){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"text");
		Sparql.removeLiteral(ETU+id, ETU+"date");
		Sparql.insertLiteral(ETU+id, ETU+"text",newText);
		Sparql.insertLiteral(ETU+id, ETU+"date", ""+date.getTime());
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * etkinliğin tarihini değiştirir
     * @param id
     * @param newDate
     * @return 
     */
    public static boolean updateEventDate(String id, Date newDate){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"date");
		Sparql.insertLiteral(ETU+id, ETU+"date",""+newDate.getTime());
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * duyuruyu oluşturan öğretmeni bulur
     * @param announceId
     * @return 
     */
    public static Teacher getAnnouncementTeacher(String announceId){
	  String query="select ?id ?email ?firstName ?surname ?status ?picture "
		    + "where {?ann rdf:type etu:Announcement. ?ann etu:id \""+announceId+"\".?ann etu:addedby ?teacher. ?teacher etu:id ?id. ?teacher etu:email ?email."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		Teacher teacher=new Teacher(toList.get(0).get("email").toString(), toList.get(0).get("id").toString(), toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(), toList.get(0).get("picture").toString());
		return teacher;
	  }
	  return null;
    }
    
    /**
     * etkinliği oluşturan öğretmeni bulur
     * @param announceId
     * @return 
     */
    public static Teacher getEventsTeacher(String announceId){
	  String query="select ?id ?email ?firstName ?surname ?status ?picture "
		    + "where {?ann rdf:type etu:Event. ?ann etu:id \""+announceId+"\".?ann etu:addedby ?teacher. ?teacher etu:id ?id. ?teacher etu:email ?email."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		Teacher teacher=new Teacher(toList.get(0).get("email").toString(), toList.get(0).get("id").toString(), toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(), toList.get(0).get("picture").toString());
		return teacher;
	  }
	  return null;
    }
    
    /**
     * duyurunun gönderildiği öğrencileri bulur
     * @param announceId
     * @return 
     */
    public static Student[] getAnnouncementStudents(String announceId){
	  String query="select ?email ?id ?firstName ?lastName ?status ?picture ?mentor where {?ann rdf:type etu:Announcement. ?ann etu:id \""+announceId+"\". ?ann etu:sendTo ?student. "
		    + "?student etu:email ?email. ?student etu:id ?id. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?lastName. "
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?mentor.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  ArrayList<Student> students=new ArrayList<Student>();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		Teacher teacher = TeacherManager.getTeacherById(next.get("mentor").toString());
		students.add(new Student(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("lastName").toString(), toList.get(0).get("status").toString(), next.get("picture").toString(), teacher));
	  }
	  Student[] array=new Student[students.size()];
	  return students.toArray(array);
    }
    
    /**
     * etkinliğin gönderildiği öğrencileri bulur
     * @param announceId
     * @return 
     */
    public static Student[] getEventsStudents(String announceId){
	  String query="select ?email ?id ?firstName ?lastName ?status ?picture ?mentor where {?ann rdf:type etu:Event. ?ann etu:id \""+announceId+"\". ?ann etu:sendTo ?student. "
		    + "?student etu:email ?email. ?student etu:id ?id. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?lastName. "
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?mentor.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  ArrayList<Student> students=new ArrayList<Student>();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		Teacher teacher = TeacherManager.getTeacherById(next.get("mentor").toString());
		students.add(new Student(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("lastName").toString(), toList.get(0).get("status").toString(), next.get("picture").toString(), teacher));
	  }
	  Student[] array=new Student[students.size()];
	  return students.toArray(array);
    }
    
    /**
     * duyuruyu oluşturanın idsini döndürür
     * @param announceId
     * @return 
     */
    public static String getAnnouncementOwnedBy(String announceId){
	  String query="select ?id where{?ann rdf:type ?Announcement. ?ann etu:id \""+announceId+"\".?ann etu:ownedBy ?course. ?course etu:id ?id}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty())
		return toList.get(0).toString();
	  return null;
    }
    
    /**
     * etkinliği oluşturanın idsini döndürür
     * @param announceId
     * @return 
     */
    public static String getEventOwnedBy(String announceId){
	  String query="select ?id where{?ann rdf:type ?Event. ?ann etu:id \""+announceId+"\".?ann etu:ownedBy ?course. ?course etu:id ?id}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty())
		return toList.get(0).toString();
	  return null;
    }
    
    /**
     * dersin duyurularını döndürür
     * @param courseId
     * @return 
     */
    public static Announcement[] getCoursesAnnouncements(String courseId){
	  String query="select ?id ?text ?teacherId where{?ann rdf:type etu:Announcement. ?ann etu:ownedBy ?course. ?course etu:id \""+courseId+"\". "
		    + "?ann etu:id ?id. ?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Announcement> announces= new ArrayList<Announcement>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		announces.add(new Announcement(querySolution.get("id").toString(), querySolution.get("text").toString(), getAnnouncementStudents(querySolution.get("id").toString()), getAnnouncementTeacher(querySolution.get("teacherId").toString()), CourseManager.getCourse(courseId)));
	  }
	  Announcement[] array=new Announcement[announces.size()];
	  return announces.toArray(array);
    }
    
    /**
     * dersin etkinliklerini döndürür
     * @param courseId
     * @return 
     */
    public static Event[] getCoursesEvents(String courseId){
	  String query="select ?id ?text ?date ?teacherId where{?ann rdf:type etu:Event. ?ann etu:ownedBy ?course. ?course etu:id \""+courseId+"\". "
		    + "?ann etu:id ?id. ?ann etu:date ?date. ?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Event> announces= new ArrayList<Event>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		announces.add(new Event(querySolution.get("id").toString(), querySolution.get("text").toString(), new Date(Long.parseLong(querySolution.get("date").toString())),getEventsStudents(querySolution.get("id").toString()), getEventsTeacher(querySolution.get("teacherId").toString()), CourseManager.getCourse(courseId)));
	  }
	  Event[] array=new Event[announces.size()];
	  return announces.toArray(array);
    }
    
    /**
     * toplulugun duyurularını döndürür.
     * @param communityId
     * @return 
     */
    public static Announcement[] getCommunityAnnouncements(String communityId){
	  String query="select ?id ?text ?teacherId where{?ann rdf:type etu:Announcement. ?ann etu:ownedBy ?comm. ?comm etu:id \""+communityId+"\". "
		    + "?ann etu:id ?id. ?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Announcement> announces= new ArrayList<Announcement>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		announces.add(new Announcement(querySolution.get("id").toString(), querySolution.get("text").toString(), getAnnouncementStudents(querySolution.get("id").toString()), getAnnouncementTeacher(querySolution.get("teacherId").toString()), CommunityManager.getCommunityById(communityId)));
	  }
	  Announcement[] array=new Announcement[announces.size()];
	  return announces.toArray(array);
    }
    
    /**
     * topluluğun etkinliklerini döndürür
     * @param communityId
     * @return 
     */
    public static Event[] getCommunityEvents(String communityId){
	  String query="select ?id ?text ?date ?teacherId where{?ann rdf:type etu:Event. ?ann etu:ownedBy ?comm. ?comm etu:id \""+communityId+"\". "
		    + "?ann etu:id ?id. ?ann etu:date ?date. ?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Event> announces= new ArrayList<Event>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		announces.add(new Event(querySolution.get("id").toString(), querySolution.get("text").toString(), new Date(Long.parseLong(querySolution.get("date").toString())),getEventsStudents(querySolution.get("id").toString()), getEventsTeacher(querySolution.get("teacherId").toString()), CommunityManager.getCommunityById(communityId)));
	  }
	  Event[] array=new Event[announces.size()];
	  return announces.toArray(array);
    }
    
    /**
     * öğrencinin duyurularını döndürür
     * @param studentId
     * @return 
     */
    public static Announcement[] getStudentsAnnouncements(String studentId){
	  String query="select ?id ?text ?teacherId where{?student rdf:type etu:Student. ?student etu:id \""+studentId+"\".?student etu:hasActivity ?ann."
		    + "?ann etu:id ?id. ?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Announcement> announces= new ArrayList<Announcement>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		if(querySolution.get("id").toString().startsWith("an")) 
		    announces.add(new Announcement(querySolution.get("id").toString(), querySolution.get("text").toString(), getAnnouncementStudents(querySolution.get("id").toString()), getAnnouncementTeacher(querySolution.get("id").toString())));
	  }
	  Announcement[] array=new Announcement[announces.size()];
	  return announces.toArray(array);
    }
    
    public static Announcement[] getTeachersAnnouncements(String studentId){
	  String query="select ?id ?text ?teacherId where{?student rdf:type etu:Teacher. ?student etu:id \""+studentId+"\".?student etu:hasActivity ?ann."
		    + "?ann etu:id ?id. ?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Announcement> announces= new ArrayList<Announcement>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		if(querySolution.get("id").toString().startsWith("an")) 
		    announces.add(new Announcement(querySolution.get("id").toString(), querySolution.get("text").toString(), getAnnouncementStudents(querySolution.get("id").toString()), getAnnouncementTeacher(querySolution.get("id").toString())));
	  }
	  Announcement[] array=new Announcement[announces.size()];
	  return announces.toArray(array);
    }
    
    /**
     * öğrencinin etkinliklerini döndürür
     * @param studentId
     * @return 
     */
    public static Event[] getStudentsEvents(String studentId){
	  String query="select ?id ?text ?date ?teacherId where{?student rdf:type etu:Student. ?student etu:id \""+studentId+"\".?student etu:hasActivity ?ann."
		    + "?ann etu:id ?id. ?ann etu:date ?date.?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Event> announces= new ArrayList<Event>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		if(querySolution.get("id").toString().startsWith("ev")) 
		    announces.add(new Event(querySolution.get("id").toString(), querySolution.get("text").toString(), new Date(Long.parseLong(querySolution.get("date").toString())), getEventsStudents(querySolution.get("id").toString()), getEventsTeacher(querySolution.get("teacherId").toString())));
	  }
	  Event[] array=new Event[announces.size()];
	  return announces.toArray(array);
    }
    
    public static Event[] getTeachersEvents(String studentId){
	  String query="select ?id ?text ?date ?teacherId where{?student rdf:type etu:Teacher. ?student etu:id \""+studentId+"\".?student etu:hasActivity ?ann."
		    + "?ann etu:id ?id. ?ann etu:date ?date.?ann etu:text ?text. ?ann etu:addedby ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  ArrayList<Event> announces= new ArrayList<Event>();
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		if(querySolution.get("id").toString().startsWith("ev")) 
		    announces.add(new Event(querySolution.get("id").toString(), querySolution.get("text").toString(), new Date(Long.parseLong(querySolution.get("date").toString())), getEventsStudents(querySolution.get("id").toString()), getEventsTeacher(querySolution.get("teacherId").toString())));
	  }
	  Event[] array=new Event[announces.size()];
	  return announces.toArray(array);
    }
    
}
